﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Collections;

using BLL;
using DAL;
using Models;
using System.Data;
using System.Text;
using System.Xml;
using Web.Old_App_Code;
using AjaxPro;

public partial class backstage_goods : System.Web.UI.Page
{
    // Fields
    protected AttributesManager am = new AttributesManager();

    protected CategoryManager cm = new CategoryManager();

    protected FreightTemplateManager ftm = new FreightTemplateManager();
    protected GoodsAttributeManager gam = new GoodsAttributeManager();
    protected GoodsCategory gc = new GoodsCategory();
    protected GoodsCategoryManager gcm = new GoodsCategoryManager();
    protected GoodsEntitysManager gem = new GoodsEntitysManager();
    protected GoodsEntitysSpecificationsManager gesm = new GoodsEntitysSpecificationsManager();
    protected GoodGalleryManager ggm = new GoodGalleryManager();
    protected GoodsManager gm = new GoodsManager();
    protected Goods gs = new Goods();
    protected GoodsTypeManager gtm = new GoodsTypeManager();
  
    protected SpecificationsValueManager sfvm = new SpecificationsValueManager();
    protected SpecificationsManager sm = new SpecificationsManager();


    // Methods
    protected StringBuilder AddCategoryBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            ArrayList list = ArrayList.Adapter(this.gm.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"])).Rows[0]["category"].ToString().Split(new char[] { ',' }));
            foreach (string str in list)
            {
                DataTable categoryByCategoryIdentity = this.cm.GetCategoryByCategoryIdentity(Convert.ToInt32(str));
                builder.AppendLine(string.Concat(new object[] { "<option value=\"", categoryByCategoryIdentity.Rows[0]["categoryIdentity"], "\">", categoryByCategoryIdentity.Rows[0]["categoryName"], "</option>" }));
            }
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder AtrributesBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable goodsById = this.gm.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
            DataTable goodsAttributeByGood = this.gam.GetGoodsAttributeByGood(Convert.ToInt32(base.Request.QueryString["id"]));
            if (goodsAttributeByGood.Rows.Count <= 0)
            {
                return builder;
            }
            builder.AppendLine("<table cellspacing=\"0\" cellpadding=\"0\">");
            foreach (DataRow row in goodsAttributeByGood.Rows)
            {
                DataTable attributeValueByAttribute;
                ArrayList list;
                DataTable table4;
                ArrayList list2;
                if (Convert.ToInt32(row["isRequired"]) == 0)
                {
                    switch (Convert.ToInt32(row["attributeInputType"]))
                    {
                        case 0:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                            goto Label_0CE4;

                        case 1:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                            goto Label_0CE4;

                        case 2:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<65 || event.keyCode>90)&&(event.keyCode<97 || event.keyCode>122)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                            goto Label_0CE4;

                        case 3:
                            attributeValueByAttribute = new AttributeValueManager().GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                            list = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                            builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                            foreach (DataRow row2 in attributeValueByAttribute.Rows)
                            {
                                if (list.Contains(row2["id"].ToString()))
                                {
                                    builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" checked=\"checked\" />" + row2["value"].ToString() + "");
                                }
                                else
                                {
                                    builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" />" + row2["value"].ToString() + "");
                                }
                            }
                            builder.AppendLine("<input name=\"hdAtrributes\" type=\"hidden\" value=\"rdAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>");
                            goto Label_0CE4;

                        case 4:
                            table4 = new AttributeValueManager().GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                            list2 = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                            builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                            foreach (DataRow row3 in table4.Rows)
                            {
                                if (list2.Contains(row3["id"].ToString()))
                                {
                                    builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" checked=\"checked\"/>" + row3["value"].ToString() + "");
                                }
                                else
                                {
                                    builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" />" + row3["value"].ToString() + "");
                                }
                            }
                            builder.AppendLine("</td></tr>");
                            goto Label_0CE4;

                        case 5:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input id=\"txtAtrributes", row["id"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" onfocus=\"new WdatePicker(this,'%Y-%M-%D',true,'default')\" style=\"ime-mode:disabled\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"0\" /></td></tr>" }));
                            goto Label_0CE4;
                    }
                }
                else
                {
                    switch (Convert.ToInt32(row["attributeInputType"]))
                    {
                        case 0:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                            goto Label_0CE4;

                        case 1:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                            goto Label_0CE4;

                        case 2:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" style=\"ime-mode:disabled\" onpaste=\"return false\" onkeydown=\"if(event.keyCode==13)event.keyCode=9\" onKeyPress=\"if ((event.keyCode<65 || event.keyCode>90)&&(event.keyCode<97 || event.keyCode>122)) event.returnValue=false\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                            goto Label_0CE4;

                        case 3:
                            attributeValueByAttribute = new AttributeValueManager().GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                            list = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                            builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                            foreach (DataRow row2 in attributeValueByAttribute.Rows)
                            {
                                if (list.Contains(row2["id"].ToString()))
                                {
                                    builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" checked=\"checked\" />" + row2["value"].ToString() + "");
                                }
                                else
                                {
                                    builder.AppendLine("<input name=\"rdAtrributes" + row["id"].ToString() + "\" type=\"radio\" value=\"" + row2["id"].ToString() + "\" />" + row2["value"].ToString() + "");
                                }
                            }
                            builder.AppendLine("<input name=\"hdAtrributes\" type=\"hidden\" value=\"rdAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>");
                            goto Label_0CE4;

                        case 4:
                            table4 = new AttributeValueManager().GetAttributeValueByAttribute(Convert.ToInt32(row["id"]));
                            list2 = ArrayList.Adapter(row["attributeValue"].ToString().Split(new char[] { ',' }));
                            builder.AppendLine("<tr><td class=\"td_title\">" + row["attributeName"] + "：</td><td class=\"td_content\">");
                            foreach (DataRow row3 in table4.Rows)
                            {
                                if (list2.Contains(row3["id"].ToString()))
                                {
                                    builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" checked=\"checked\" />" + row3["value"].ToString() + "");
                                }
                                else
                                {
                                    builder.AppendLine("<input type=\"checkbox\" name=\"cbAtrributes" + row["id"].ToString() + "\" value=\"" + row3["id"].ToString() + "\" />" + row3["value"].ToString() + "");
                                }
                            }
                            builder.AppendLine("<input name=\"hdAtrributes\" type=\"hidden\" value=\"cbAtrributes" + row["id"].ToString() + "\" title=\"" + row["attributeName"].ToString() + "\" /></td></tr>");
                            goto Label_0CE4;

                        case 5:
                            builder.AppendLine(string.Concat(new object[] { "<tr><td class=\"td_title\">", row["attributeName"], "：</td><td class=\"td_content\"><input title=\"", row["attributeName"].ToString(), "\" name=\"atrributesValue\" type=\"text\" value=\"", row["inputValue"].ToString(), "\" onfocus=\"new WdatePicker(this,'%Y-%M-%D',true,'default')\" style=\"ime-mode:disabled\" /><input name=\"hdAtrributesValue\" type=\"hidden\" value=\"1\" /></td></tr>" }));
                            goto Label_0CE4;
                    }
                }
            Label_0CE4: ;
            }
            builder.AppendLine("</table>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder BrandBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable goodsById = this.gm.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
            Array array = goodsById.Rows[0]["category"].ToString().Split(new char[] { ',' });
            DataTable table2 = new DataTable();
            ArrayList list = new ArrayList();
            bool flag = false;
            foreach (string str in array)
            {
                DataTable brandByCategoryIdentity = new BrandManager().GetBrandByCategoryIdentity(Convert.ToInt32(str));
                if (!flag)
                {
                    table2 = brandByCategoryIdentity.Clone();
                    table2.Clear();
                    flag = true;
                }
                foreach (DataRow row in brandByCategoryIdentity.Rows)
                {
                    if (!list.Contains(row["id"].ToString()))
                    {
                        list.Add(row["id"].ToString());
                        table2.Rows.Add(row.ItemArray);
                    }
                }
            }
            foreach (DataRow row in table2.Rows)
            {
                if (Convert.ToInt32(goodsById.Rows[0]["brand"]) == Convert.ToInt32(row["id"]))
                {
                    builder.AppendLine("<option value=\"" + row["id"].ToString() + "\" selected=\"selected\" >" + row["brandName"].ToString() + "</option>");
                }
                else
                {
                    builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["brandName"].ToString() + "</option>");
                }
            }
        }
        catch
        {
        }
        return builder;
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            this.gs.CategoryId = base.Request.Form["addCategory"];
            this.gs.Brand = Convert.ToInt32(base.Request.Form["brand"]);
            this.gs.GoodsType = Convert.ToInt32(base.Request.Form["sGoodsType"]);
            this.gs.GoodsName = this.txtGoodsName.Value;
            this.gs.GoodsNo = this.txtGoodsNo.Value;
            this.gs.Origin = this.txtOrigin.Value;
            this.gs.IsBest = Convert.ToInt32(base.Request.Form["isBest"]);
            this.gs.IsNew = Convert.ToInt32(base.Request.Form["isNew"]);
            this.gs.IsTopSelling = Convert.ToInt32(base.Request.Form["isTopSelling"]);
            this.gs.IsShelves = Convert.ToInt32(base.Request.Form["isShelves"]);
            this.gs.IsPromote = Convert.ToInt32(base.Request.Form["isPromote"]);
            this.gs.IsUserDiscount = Convert.ToInt32(base.Request.Form["isUserDiscount"]);
            this.gs.IsOrderFromFreight = this.cbIsOrderFromFreight.Checked ? 1 : 0;
            this.gs.IsDiscount = this.cbIsDiscount.Checked ? 1 : 0;
            this.gs.IsTotalReduce = this.cbIsTotalReduce.Checked ? 1 : 0;
            this.gs.IsGifts = this.cbIsGifts.Checked ? 1 : 0;
            this.gs.IsSendIntegral = this.cbIsSendIntegral.Checked ? 1 : 0;
            this.gs.IsFromFreight = Convert.ToInt32(base.Request.Form["isFromFreight"]);
            this.gs.FreightTemplate = (this.gs.IsFromFreight == 1) ? 0 : Convert.ToInt32(base.Request.Form["freightTemplate"]);
            this.gs.Keywords = this.txtKeywords.Value;
            this.gs.Description = this.txtaDescription.Value;
            this.gs.LanguageVersion = this.Session["language_version"].ToString();
            this.gs.GoodsRemark = this.fckGoodsRemark.Value;
            this.gs.Id = Convert.ToInt32(base.Request.QueryString["id"]);
            if (this.gm.ModifyGoods(this.gs))
            {
                int num;
                ArrayList list = ArrayList.Adapter(this.gs.CategoryId.Split(new char[] { ',' }));
                ArrayList list2 = new ArrayList();
                DataTable goodsCategoryByGoods = this.gcm.GetGoodsCategoryByGoods(this.gs.Id);
                this.gc.Goods = this.gs.Id;
                foreach (DataRow row in goodsCategoryByGoods.Rows)
                {
                    list2.Add(row["category"].ToString());
                    if (!list.Contains(row["category"].ToString()))
                    {
                        this.gcm.DeleteGoodsCategory(Convert.ToInt32(row["id"]));
                    }
                }
                foreach (string str in list)
                {
                    if (!list2.Contains(str))
                    {
                        this.gc.Category = Convert.ToInt32(str);
                        this.gcm.AddGoodsCategory(this.gc);
                    }
                }
                ArrayList list3 = ArrayList.Adapter(base.Request.Form["hdGoodsEntitys"].Split(new char[] { ',' }));
                ArrayList list4 = ArrayList.Adapter(base.Request.Form["barCode"].Split(new char[] { ',' }));
                ArrayList list5 = ArrayList.Adapter(base.Request.Form["goodsArtno"].Split(new char[] { ',' }));
                ArrayList list6 = ArrayList.Adapter(base.Request.Form["salePrice"].Split(new char[] { ',' }));
                ArrayList list7 = ArrayList.Adapter(base.Request.Form["costPrice"].Split(new char[] { ',' }));
                ArrayList list8 = ArrayList.Adapter(base.Request.Form["marketPrice"].Split(new char[] { ',' }));
                ArrayList list9 = ArrayList.Adapter(base.Request.Form["giveIntegral"].Split(new char[] { ',' }));
                ArrayList list10 = ArrayList.Adapter(base.Request.Form["goodsWeight"].Split(new char[] { ',' }));
                ArrayList list11 = ArrayList.Adapter(base.Request.Form["selectWeightUnit"].Split(new char[] { ',' }));
                ArrayList list12 = ArrayList.Adapter(base.Request.Form["inventory"].Split(new char[] { ',' }));
                for (num = 0; num < list3.Count; num++)
                {
                    GoodsEntitysSpecifications specifications;
                    DataTable specificationsByLanguageVersion;
                    GoodsEntitys goodsEntitys = new GoodsEntitys();
                    goodsEntitys.Goods = this.gs.Id;
                    goodsEntitys.BarCode = list4[num].ToString();
                    goodsEntitys.GoodsArtno = list5[num].ToString();
                    goodsEntitys.SalePrice = Convert.ToDecimal(list6[num]);
                    goodsEntitys.CostPrice = Convert.ToDecimal(list7[num]);
                    goodsEntitys.MarketPrice = Convert.ToDecimal(list8[num]);
                    goodsEntitys.Integral = 0;
                    goodsEntitys.GiveIntegral = Convert.ToInt32(list9[num]);
                    goodsEntitys.GoodsWeight = Convert.ToDecimal(list10[num]);
                    goodsEntitys.WeightUnit = list11[num].ToString();
                    goodsEntitys.Inventory = Convert.ToInt32(list12[num]);
                    if (Convert.ToInt32(list3[num]) == 0)
                    {
                        goodsEntitys.Id = this.gem.AddGoodsEntitys(goodsEntitys);
                        if (goodsEntitys.Id != 0)
                        {
                            specifications = new GoodsEntitysSpecifications();
                            specificationsByLanguageVersion = this.sm.GetSpecificationsByLanguageVersion(this.Session["language_version"].ToString());
                            foreach (DataRow row in specificationsByLanguageVersion.Rows)
                            {
                                if (base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""] != null)
                                {
                                    specifications.GoodsEntitys = goodsEntitys.Id;
                                    specifications.Specifications = Convert.ToInt32(row["id"]);
                                  //  specifications.SpecificationsValue = Convert.ToInt32(ArrayList.Adapter(base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""].Split(new char[] { 0x2c }))[num]);
                                    this.gesm.AddGoodsEntitysSpecifications(specifications);
                                }
                            }
                        }
                    }
                    else
                    {
                        goodsEntitys.Id = Convert.ToInt32(list3[num]);
                        if (this.gem.ModifyGoodsEntitys(goodsEntitys))
                        {
                            specifications = new GoodsEntitysSpecifications();
                            specificationsByLanguageVersion = this.sm.GetSpecificationsByLanguageVersion(this.Session["language_version"].ToString());
                            this.gesm.DeleteGoodsEntitysSpecificationsByGoodsEntitys(goodsEntitys.Id);
                            foreach (DataRow row in specificationsByLanguageVersion.Rows)
                            {
                                if (base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""] != null)
                                {
                                    specifications.GoodsEntitys = goodsEntitys.Id;
                                    specifications.Specifications = Convert.ToInt32(row["id"]);
                                   // specifications.SpecificationsValue = Convert.ToInt32(ArrayList.Adapter(base.Request.Form["SpecificationsValue" + row["id"].ToString() + ""].Split(new char[] { 0x2c }))[num]);
                                    this.gesm.AddGoodsEntitysSpecifications(specifications);
                                }
                            }
                        }
                    }
                }
                HttpFileCollection files = HttpContext.Current.Request.Files;
                if (base.Request.Form["imgAlt"] != null)
                {
                    ArrayList list13 = ArrayList.Adapter(base.Request.Form["imgAlt"].Split(new char[] { ',' }));
                    ArrayList list14 = ArrayList.Adapter(base.Request.Form["hdGoodGallery"].Split(new char[] { ',' }));
                    for (num = 0; num < files.Count; num++)
                    {
                        GoodGallery goodGallery = new GoodGallery();
                        goodGallery.Goods = this.gs.Id;
                        if (Convert.ToInt32(list14[num]) != 0)
                        {
                            DataTable table3;
                            table3 = table3 = this.ggm.GetGoodGalleryById(Convert.ToInt32(list14[num]));
                            goodGallery.Id = Convert.ToInt32(table3.Rows[0]["id"]);
                            if (files[num].FileName != "")
                            {
                                File.Delete(base.Server.MapPath(table3.Rows[0]["thumbImg"].ToString()));
                                File.Delete(base.Server.MapPath(table3.Rows[0]["smallImg"].ToString()));
                                File.Delete(base.Server.MapPath(table3.Rows[0]["bigImg"].ToString()));
                            }
                            else
                            {
                                goodGallery.BigImg = table3.Rows[0]["bigImg"].ToString();
                                goodGallery.SmallImg = table3.Rows[0]["smallImg"].ToString();
                                goodGallery.ThumbImg = table3.Rows[0]["thumbImg"].ToString();
                            }
                        }
                        if (files[num].FileName != "")
                        {
                            string str2 = DateTime.Now.ToString("yyyyMMddHHssffffff");
                            goodGallery.BigImg = "/cn/goods/big/" + str2 + Path.GetExtension(files[num].FileName);
                            goodGallery.SmallImg = "/cn/goods/small/" + str2 + Path.GetExtension(files[num].FileName);
                            goodGallery.ThumbImg = "/cn/goods/thumb/" + str2 + Path.GetExtension(files[num].FileName);
                            files[num].SaveAs(base.Server.MapPath(goodGallery.BigImg));
                            ThumbnailImage image = new ThumbnailImage();
                            image.GetThumbnail(base.Server.MapPath(goodGallery.BigImg), base.Server.MapPath(goodGallery.SmallImg), 500, 500, 1, System.Drawing.Color.White);
                            image.GetThumbnail(base.Server.MapPath(goodGallery.BigImg), base.Server.MapPath(goodGallery.ThumbImg), 200, 200, 1, System.Drawing.Color.White);
                        }
                        goodGallery.ImgAlt = list13[num].ToString();
                        if (Convert.ToInt32(list14[num]) != 0)
                        {
                            this.ggm.ModifyGoodGallery(goodGallery);
                        }
                        else
                        {
                            this.ggm.AddGoodGallery(goodGallery);
                        }
                    }
                }
                this.gam.DeleteGoodsAttributeByGoods(this.gs.Id);
                DataTable atrributesByGoodsTypeAndState = this.am.GetAtrributesByGoodsTypeAndState(this.gs.GoodsType, 1);
                int num2 = 0;
                if (atrributesByGoodsTypeAndState.Rows.Count > 0)
                {
                    ArrayList list15 = new ArrayList();
                    if (base.Request.Form["atrributesValue"] != null)
                    {
                        list15 = ArrayList.Adapter(base.Request.Form["atrributesValue"].Split(new char[] { ',' }));
                    }
                    for (int i = 0; i < atrributesByGoodsTypeAndState.Rows.Count; i++)
                    {
                        GoodsAttribute goodsAttribute = new GoodsAttribute();
                        goodsAttribute.Goods = this.gs.Id;
                        goodsAttribute.Attributes = Convert.ToInt32(atrributesByGoodsTypeAndState.Rows[i]["id"]);
                        switch (Convert.ToInt32(atrributesByGoodsTypeAndState.Rows[i]["attributeInputType"]))
                        {
                            case 0:
                            case 1:
                            case 2:
                            case 5:
                                goodsAttribute.InputValue = (list15.Count > num2) ? list15[num2].ToString() : "";
                                goodsAttribute.AttributeValue = "0";
                                this.gam.AddGoodsAttribute(goodsAttribute);
                                num2++;
                                break;

                            case 3:
                                {
                                    string str3 = "rdAtrributes" + atrributesByGoodsTypeAndState.Rows[i]["id"].ToString();
                                    goodsAttribute.AttributeValue = base.Request.Form[str3];
                                    goodsAttribute.InputValue = "";
                                    this.gam.AddGoodsAttribute(goodsAttribute);
                                    break;
                                }
                            case 4:
                                {
                                    string str4 = "cbAtrributes" + atrributesByGoodsTypeAndState.Rows[i]["id"].ToString();
                                    goodsAttribute.AttributeValue = base.Request.Form[str4];
                                    goodsAttribute.InputValue = "";
                                    this.gam.AddGoodsAttribute(goodsAttribute);
                                    break;
                                }
                        }
                    }
                }
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新成功！\");location.href=\"goods_list.aspx\";</script>");
            }
            else
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
            }
        }
        catch
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"更新失败！\");</script>");
        }
    }

    protected StringBuilder CategoryBind()
    {
        StringBuilder sb = new StringBuilder();
        try
        {
            this.RecursionCategory(sb, "00000000-0000-0000-0000-000000000000", this.Session["language_version"].ToString());
        }
        catch
        {
        }
        return sb;
    }

    protected StringBuilder FreightTemplateBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable freightTemplateByLanguageVersion = this.ftm.GetFreightTemplateByLanguageVersion(this.Session["language_version"].ToString());
            DataTable goodsById = this.gm.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
            builder.AppendLine("<select id=\"selectFreightTemplate\" name=\"freightTemplate\" onchange=\"setFreightTemplate();\">");
            builder.AppendLine("<option value=\"0\">请选择运费模版...</option>");
            foreach (DataRow row in freightTemplateByLanguageVersion.Rows)
            {
                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row["id"], "\">", row["freightTemplateName"], "</option>" }));
            }
            builder.AppendLine("</select>");
            if (Convert.ToInt32(goodsById.Rows[0]["isFromFreight"]) == 0)
            {
                builder.AppendLine(string.Concat(new object[] { "<span id=\"spFreightTemplate\"><a href=\"freight_way_list.aspx?tid=", goodsById.Rows[0]["freightTemplate"], "&gId=", base.Request.QueryString["id"], "\">设置运费方式</a></span>" }));
                return builder;
            }
            builder.AppendLine("<span id=\"spFreightTemplate\"></span>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder GoodGalleryBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable goodGalleryByGoods = this.ggm.GetGoodGalleryByGoods(Convert.ToInt32(base.Request.QueryString["id"]));
            foreach (DataRow row in goodGalleryByGoods.Rows)
            {
                builder.AppendLine("<tr>");
                builder.AppendLine(string.Concat(new object[] { "<td><input name=\"hdGoodGallery\" type=\"hidden\" value=\"", row["id"], "\" /><img src=\"", row["thumbImg"].ToString(), "\" alt=\"", row["imgAlt"].ToString(), "\" style=\"width:50px; height:50px;\" /><br /><input name=\"fileGoodGallery\" type=\"file\" /><span class=\"imgClass\"> *分辨率大于500*500的等比例图片</span></td>" }));
                builder.AppendLine("<td><input name=\"imgAlt\" type=\"text\" value=\"" + row["imgAlt"] + "\" /></td>");
                builder.AppendLine("<td><input type=\"button\" class=\"formButton\" value=\"删除\" title=\"删除图片\" onclick=\"delGalleryInput(this.parentNode.parentNode.rowIndex," + row["id"].ToString() + ");\"/></td>");
                builder.AppendLine("</tr>");
            }
        }
        catch
        {
        }
        return builder;
    }

    private void GoodsBind()
    {
        try
        {
            DataTable goodsById = this.gm.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
            if (goodsById.Rows.Count > 0)
            {
                DataRow row = goodsById.Rows[0];
                this.txtGoodsName.Value = row["goodsName"].ToString();
                this.txtGoodsNo.Value = row["goodsNo"].ToString();
                this.txtOrigin.Value = row["origin"].ToString();
                if (Convert.ToInt32(row["isBest"]) == 0)
                {
                    this.rdIsBest0.Checked = true;
                }
                else
                {
                    this.rdIsBest1.Checked = true;
                }
                if (Convert.ToInt32(row["isNew"]) == 0)
                {
                    this.rdIsNew0.Checked = true;
                }
                else
                {
                    this.rdIsNew1.Checked = true;
                }
                if (Convert.ToInt32(row["isTopSelling"]) == 0)
                {
                    this.rdIsTopSelling0.Checked = true;
                }
                else
                {
                    this.rdIsTopSelling1.Checked = true;
                }
                if (Convert.ToInt32(row["isShelves"]) == 0)
                {
                    this.rdIsShelves0.Checked = true;
                }
                else
                {
                    this.rdIsShelves1.Checked = true;
                }
                if (Convert.ToInt32(row["isPromote"]) == 0)
                {
                    this.rdIsPromote0.Checked = true;
                }
                else
                {
                    this.rdIsPromote1.Checked = true;
                }
                if (Convert.ToInt32(row["isUserDiscount"]) == 0)
                {
                    this.rdIsUserDiscount0.Checked = true;
                }
                else
                {
                    this.rdIsUserDiscount1.Checked = true;
                }
                if (Convert.ToInt32(row["isOrderFromFreight"]) == 0)
                {
                    this.cbIsOrderFromFreight.Checked = false;
                }
                else
                {
                    this.cbIsOrderFromFreight.Checked = true;
                }
                if (Convert.ToInt32(row["isDiscount"]) == 0)
                {
                    this.cbIsDiscount.Checked = false;
                }
                else
                {
                    this.cbIsDiscount.Checked = true;
                }
                if (Convert.ToInt32(row["isTotalReduce"]) == 0)
                {
                    this.cbIsTotalReduce.Checked = false;
                }
                else
                {
                    this.cbIsTotalReduce.Checked = true;
                }
                if (Convert.ToInt32(row["isGifts"]) == 0)
                {
                    this.cbIsGifts.Checked = false;
                }
                else
                {
                    this.cbIsGifts.Checked = true;
                }
                if (Convert.ToInt32(row["isSendIntegral"]) == 0)
                {
                    this.cbIsSendIntegral.Checked = false;
                }
                else
                {
                    this.cbIsSendIntegral.Checked = true;
                }
                if (Convert.ToInt32(row["isFromFreight"]) == 0)
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sFT", "<script language=\"javascript\" defer>$(\"#trFreightTemplate\").show();selectIsExitItem(\"selectFreightTemplate\",\"" + row["freightTemplate"].ToString() + "\");</script>");
                    this.rdIsFromFreight0.Checked = true;
                }
                else
                {
                    base.ClientScript.RegisterStartupScript(base.GetType(), "sFT", "<script language=\"javascript\" defer>$(\"#trFreightTemplate\").hide();</script>");
                    this.rdIsFromFreight1.Checked = true;
                }
                this.txtKeywords.Value = row["keywords"].ToString();
                this.txtaDescription.Value = row["description"].ToString();
                this.fckGoodsRemark.Value = row["goodsRemark"].ToString();
                this.txtGoodsName.Focus();
            }
        }
        catch
        {
        }
    }

    protected StringBuilder GoodsEntitysBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataRow current;
            DataTable goodsEntitysByGoods = this.gem.GetGoodsEntitysByGoods(Convert.ToInt32(base.Request.QueryString["id"]));
            DataTable specificationsNameByGoodsEntitys = new DataTable();
            if (goodsEntitysByGoods.Rows.Count > 0)
            {
                specificationsNameByGoodsEntitys = this.sm.GetSpecificationsNameByGoodsEntitys(Convert.ToInt32(goodsEntitysByGoods.Rows[0]["id"]));
            }
            builder.AppendLine("<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" id=\"GoodsEntitysSpecifications\">");
            builder.AppendLine("<tr><td>条形码</td><td>货号</td>");
        IEnumerator enumerator = specificationsNameByGoodsEntitys.Rows.GetEnumerator();

                while (enumerator.MoveNext())
                {
                    current = (DataRow)enumerator.Current;
                    builder.AppendLine("<td><span id=\"spanS" + current["id"].ToString() + "\">" + current["specificationsName"].ToString() + "</span></td>");
                }
      
            builder.AppendLine("<td>销售价</td><td>成本价</td><td>市场价</td><td>赠送积分</td><td>重量</td><td>重量单位</td><td>库存</td><td>操作</td></tr> ");
            for (int i = 0; i < goodsEntitysByGoods.Rows.Count; i++)
            {
                current = goodsEntitysByGoods.Rows[i];
                builder.AppendLine(string.Concat(new object[] { "<tr><td><input name=\"hdGoodsEntitys\" type=\"hidden\" value=\"", current["id"], "\" /><input name=\"barCode\" type=\"text\" class=\"specifications\" value=\"", current["barCode"].ToString(), "\" /></td>" }));
                builder.AppendLine("<td><input name=\"goodsArtno\" type=\"text\" class=\"specifications\" value=\"" + current["goodsArtno"].ToString() + "\" /></td>");
                foreach (DataRow row2 in specificationsNameByGoodsEntitys.Rows)
                {
                    DataTable specificationsValueBySpecifications = this.sfvm.GetSpecificationsValueBySpecifications(Convert.ToInt32(row2["id"]));
                    builder.AppendLine("<td><select name=\"SpecificationsValue" + row2["id"].ToString() + "\">");
                    DataTable goodsEntitysSpecificationsByGoodsEntitysAndSpecifications = this.gesm.GetGoodsEntitysSpecificationsByGoodsEntitysAndSpecifications(Convert.ToInt32(current["id"]), Convert.ToInt32(row2["id"]));
                    foreach (DataRow row3 in specificationsValueBySpecifications.Rows)
                    {
                        if (goodsEntitysSpecificationsByGoodsEntitysAndSpecifications.Rows.Count > 0)
                        {
                            if (Convert.ToInt32(row3["id"]) == Convert.ToInt32(goodsEntitysSpecificationsByGoodsEntitysAndSpecifications.Rows[0]["specificationsValue"]))
                            {
                                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row3["id"], "\" selected=\"selected\">", row3["specificationsValueName"], "</option>" }));
                            }
                            else
                            {
                                builder.AppendLine(string.Concat(new object[] { "<option value=\"", row3["id"], "\">", row3["specificationsValueName"], "</option>" }));
                            }
                        }
                        else
                        {
                            builder.AppendLine(string.Concat(new object[] { "<option value=\"", row3["id"], "\">", row3["specificationsValueName"], "</option>" }));
                        }
                    }
                    builder.AppendLine("</select></td>");
                }
                builder.AppendLine("<td><input name=\"salePrice\" type=\"text\" class=\"specifications\" value=\"" + current["salePrice"].ToString() + "\" /></td>");
                builder.AppendLine("<td><input name=\"costPrice\" type=\"text\" class=\"specifications\" value=\"" + current["costPrice"].ToString() + "\" /></td>");
                builder.AppendLine("<td><input name=\"marketPrice\" type=\"text\" class=\"specifications\" value=\"" + current["marketPrice"].ToString() + "\" /></td>");
                builder.AppendLine("<td><input name=\"giveIntegral\" type=\"text\" class=\"specifications\" value=\"" + current["giveIntegral"].ToString() + "\" /></td>");
                builder.AppendLine("<td><input name=\"goodsWeight\" type=\"text\" class=\"specifications\" value=\"" + current["goodsWeight"].ToString() + "\" /></td>");
                if (current["weightUnit"].ToString() == "克")
                {
                    builder.AppendLine("<td><select name=\"selectWeightUnit\"><option value=\"克\" selected=\"selected\">克 </option><option value=\"千克\">千克 </option><option value=\"吨\">吨 </option></select></td>");
                }
                else if (current["weightUnit"].ToString() == "千克")
                {
                    builder.AppendLine("<td><select name=\"selectWeightUnit\"><option value=\"克\">克 </option><option value=\"千克\" selected=\"selected\">千克 </option><option value=\"吨\">吨 </option></select></td>");
                }
                else
                {
                    builder.AppendLine("<td><select name=\"selectWeightUnit\"><option value=\"克\">克 </option><option value=\"千克\">千克 </option><option value=\"吨\" selected=\"selected\">吨 </option></select></td>");
                }
                builder.AppendLine("<td><input name=\"inventory\" type=\"text\" class=\"specifications\" value=\"" + current["inventory"].ToString() + "\" /></td>");
                if (i == 0)
                {
                    builder.AppendLine("<td><input id=\"btnAddGoodsEntitys\" type=\"button\" class=\"formButton\" value=\"添加\" title=\"添加货品\" onclick=\"AddGoodsEntitys();\" /></td>");
                }
                else
                {
                    builder.AppendLine("<td><input type=\"button\" class=\"formButton\" value=\"删除\" title=\"删除货品\" onclick=\"delGoodsEntitys(this.parentNode.parentNode.rowIndex," + current["id"].ToString() + ");\"></td>");
                }
            }
            builder.AppendLine("</table>");
        }
        catch
        {
        }
        return builder;
    }

    protected StringBuilder GoodsTypeBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable goodsById = this.gm.GetGoodsById(Convert.ToInt32(base.Request.QueryString["id"]));
            DataTable goodsTypeByLanguageVersion = this.gtm.GetGoodsTypeByLanguageVersion(this.Session["language_version"].ToString());
            foreach (DataRow row in goodsTypeByLanguageVersion.Rows)
            {
                if (Convert.ToInt32(goodsById.Rows[0]["goodsType"]) == Convert.ToInt32(row["id"]))
                {
                    builder.AppendLine("<option value=\"" + row["id"].ToString() + "\" selected=\"selected\">" + row["typeName"].ToString() + "</option>");
                }
                else
                {
                    builder.AppendLine("<option value=\"" + row["id"].ToString() + "\">" + row["typeName"].ToString() + "</option>");
                }
            }
        }
        catch
        {
        }
        return builder;
    }

    protected override object LoadPageStateFromPersistenceMedium()
    {
        byte[] inArray = ViewStateCompression.DeCompress(Convert.FromBase64String(base.Request.Form.Get("__MSPVSTATE")));
        LosFormatter formatter = new LosFormatter();
        return formatter.Deserialize(Convert.ToBase64String(inArray));
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
        {
            base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
        }
        Utility.RegisterTypeForAjax(typeof(Goods_ajax));
        if (!(base.IsPostBack || (base.Request.QueryString["id"] == null)))
        {
            this.GoodsBind();
        }
    }

    private void RecursionCategory(StringBuilder sb, string parentCategory, string languageVersion)
    {
        DataTable table = this.cm.GetCategoryByParentIdAndLanguageVersionAndIsShow(parentCategory, languageVersion, -1);
        if (table.Rows.Count > 0)
        {
            foreach (DataRow row in table.Rows)
            {
                string str = "";
                int num = Convert.ToInt32(row["depth"]);
                if (num > 0)
                {
                    for (int i = 0; i < num; i++)
                    {
                        str = str + "&nbsp;&nbsp;&nbsp;&nbsp;";
                    }
                    str = str + "┣";
                }
                sb.AppendLine("<option value=\"" + row["categoryIdentity"].ToString() + "\">" + str + row["categoryName"].ToString() + "</option>");
                this.RecursionCategory(sb, row["id"].ToString(), languageVersion);
            }
        }
    }

    protected override void SavePageStateToPersistenceMedium(object pViewState)
    {
        LosFormatter formatter = new LosFormatter();
        StringWriter output = new StringWriter();
        formatter.Serialize(output, pViewState);
        string hiddenFieldInitialValue = Convert.ToBase64String(ViewStateCompression.Compress(Convert.FromBase64String(output.ToString())));
        base.ClientScript.RegisterHiddenField("__MSPVSTATE", hiddenFieldInitialValue);
    }

    protected StringBuilder SpecificationsBind()
    {
        StringBuilder builder = new StringBuilder();
        try
        {
            DataTable specificationsByLanguageVersion = this.sm.GetSpecificationsByLanguageVersion(this.Session["language_version"].ToString());
            DataTable goodsEntitysSpecificationsByGoods = this.gesm.GetGoodsEntitysSpecificationsByGoods(Convert.ToInt32(base.Request.QueryString["id"]));
            ArrayList list = new ArrayList();
            foreach (DataRow row in goodsEntitysSpecificationsByGoods.Rows)
            {
                list.Add(Convert.ToInt32(row["specifications"]));
            }
            bool flag = false;
            foreach (DataRow row in specificationsByLanguageVersion.Rows)
            {
                builder.AppendLine("" + (!flag ? "" : "<br />") + "<input id=\"cb" + row["id"].ToString() + "\" value=\"" + row["id"].ToString() + "\" name=\"ss\" type=\"checkbox\" " + (list.Contains(Convert.ToInt32(row["id"])) ? "checked='checked'" : "") + " onclick=\"getSpecifications(" + row["id"].ToString() + ",this);\" />[" + row["specificationsName"].ToString() + "]");
                flag = true;
            }
        }
        catch
        {
        }
        return builder;
    }

}